package com.swxc.smartorder.ui.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.swxc.smartorder.R;
import com.swxc.smartorder.utils.DisplayUtil;

/**
 * Author   Administrator
 * Date     2018/4/2
 * Desc     eg
 *
 * @author Administrator
 */
public class LoadingDialog extends DialogFragment {

    public static boolean isShow = false;
    private Activity mActivity;
    private ImageView mIvLoading;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShow = true;
        mActivity = this.getActivity();
        setStyle(android.support.v4.app.DialogFragment.STYLE_NO_TITLE, R.style.AlertDialogStyle);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }


    @Override
    public void onStart() {
        super.onStart();

        getDialog().setCanceledOnTouchOutside(false);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics d = mActivity.getResources().getDisplayMetrics();
        lp.width = DisplayUtil.INSTANCE.dip2px(mActivity, 130);
        lp.height = DisplayUtil.INSTANCE.dip2px(mActivity, 130);
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();

        return inflater.inflate(R.layout.layout_loading, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        mIvLoading = view.findViewById(R.id.iv_loading);
        Animation circleAnim = AnimationUtils.loadAnimation(mActivity, R.anim.anim_round_rotate);
        //设置匀速旋转，在xml文件中设置会出现卡顿
        LinearInterpolator interpolator = new LinearInterpolator();
        circleAnim.setInterpolator(interpolator);
        //开始动画
        mIvLoading.startAnimation(circleAnim);

    }

    @Override
    public void onDismiss(DialogInterface dialog) {

        super.onDismiss(dialog);

        mIvLoading.clearAnimation();
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}